//
//  UIImage+Extension.swift
//  Weibo8023
//
//  Created by styshy on 15/12/19.
//  Copyright © 2015年 sz. All rights reserved.
//

import UIKit

extension UIImage{
    
    
    // 将图片缩放到指定的宽度
    func scaleToWidth(width:CGFloat) -> UIImage{
        if size.width < width{
            return self
        }
        
        let h:CGFloat = size.height * width / size.width
        
        let rect:CGRect = CGRect(x: 0, y: 0, width:width, height: h)
        
        // 1.开启图片上下文
        UIGraphicsBeginImageContext(rect.size)
        
        // 2.绘制图片内容
        self.drawInRect(rect)
        
        // 3.获取绘制结果
        let result = UIGraphicsGetImageFromCurrentImageContext()
        
        // 4.关闭上下文
        UIGraphicsEndImageContext()
        
        
        return result
    }
}